home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 32 / Amiga Format AFCD32 (Nov 1998, Issue 117).iso / -seriously_amiga- / programming / basic / textreader / help.asc < prev    next >
Text File  |  1998-08-10  |  8KB  |  235 lines

  1. ;GT text reader
  2. ;by Curt Esser
  3. ;camge@ix.netcom.com
  4. ;last modified March 5,1998
  5.  
  6. ;MUST HAVE amigalibs.res  as resident in compiler options!
  7.  
  8. ;--------------------------------------------------------------
  9.  
  10. WBStartup
  11. NoCli
  12.  
  13.  
  14. width.w=640                     ;screen width
  15. height.w=400                    ;screen height
  16.  
  17.  
  18. ;--------------SET UP HELP WINDOW VARIABLES--------------------
  19.  
  20. HelpMe.b=Off                   ;starts with help window closed
  21. HelpGadgets.b=2                ;0=none,1=cycle only,2=both
  22.  
  23. NEWTYPE.textEX                 ;for the word-wrapping stuff
  24.   txwidth.w                    ;this is used as a workspace
  25.   txheight.w                   ;by the Textfit_ command
  26.   txMinx.w
  27.   txMiny.w
  28.   txMaxx.w
  29.   txMaxy.w
  30. End NEWTYPE
  31.  
  32. NEWTYPE.text                   ;for the GTlistview
  33.   pad.w
  34.   text.s
  35. End NEWTYPE
  36.  
  37. al.textEX\txwidth=0,0,0,0,0,0  ;for the word-wrap routine
  38.  
  39. maxlines=2000                  ;must be > maximum lines of text expected
  40.  
  41. Dim List htxt.text(maxlines)   ;for the listview gadget
  42.  
  43. font$="topaz.font"             ;name of the font for the help window
  44. fsize.b=11                     ;font height to be used
  45. #HelpWindow=1                  ;window number for the help window
  46. help$="GT.help"                ;name & path of help text file
  47. helpwide.w=500                 ;width of help window
  48. helpheight.w=10                ;height of text window in text lines!
  49. helpx.w=(width/2)-(helpwide/2) ;inital x position for help window
  50. helpy.w=(height/2)-(helpheight*fsize/2)  ;initial y position for help window
  51. Dim links.w(50)                ;maximum # of index links
  52. Dim lnk$(50)                   ;string array to store the link labels
  53.  
  54. helphigh=(helpheight+1)*fsize+9  ;calculate height of help window
  55. lvhigh.w=(helpheight+1)*fsize    ;calculate height of listview gadget
  56. hghigh.w=fsize+8               ;calculate height of help gadgets
  57. If HelpGadgets>0
  58.   helphigh+fsize+8             ;make the window tall enough
  59. EndIf                          ;so the gadgets will fit too
  60.  
  61. cy$="Top"                      ;first face for cycle gadget
  62. txlim.w=helpwide-22            ;limit of text printing width
  63.  
  64. ;-------------------------------------------------------------------
  65.  
  66.  
  67. LoadFont 0,font$,fsize         ;load the font
  68. Use IntuiFont 0
  69. Screen 0,0,0,width,height,4,$29004,"Help Test Screen",1,0
  70. AddIDCMP $400
  71. Window 0,20,20,400,80,$10000,"Help Test",0,0
  72.  
  73. CatchDosErrs                   ;put requestors on our screen
  74.  
  75. Use Window 0
  76. WindowFont 0
  77. Activate 0
  78.  
  79. NPrint "Press HELP to toggle help window"
  80. NPrint "Press ESC to Quit"
  81.  
  82.  
  83. .mainloop
  84. Repeat
  85.   ev.l=WaitEvent
  86.   If EventWindow=#HelpWindow                    ;the help window
  87.     If ev=$40                                   ;a gadget was pressed
  88.       If GadgetHit=52                           ;selector cycle gad
  89.         ec=EventCode                            ;which face is up?
  90.         gt.w=links(ec)                          ;reset the GoTo line
  91.         If HelpGadgets=1
  92.           GTSetAttrs #HelpWindow,50,#GTLV_Top,gt;reset the listview
  93.         EndIf
  94.       EndIf
  95.       If GadgetHit=51                           ;Go To gadget
  96.         GTSetAttrs #HelpWindow,50,#GTLV_Top,gt  ;reset listview to selected line
  97.       EndIf
  98.     EndIf
  99.     If ev=$200 Then Gosub helppressed           ;close gadget pressed
  100.   EndIf
  101.  
  102.   If ev=$400                                    ;key pressed
  103.  
  104.       a$=Inkey$
  105.       If a$=Chr$(27)                            ;ESC
  106.         Request "","Later...","Bye!"
  107.         split.b=1
  108.       EndIf
  109.       If a$=Chr$(139)                           ;Help key
  110.           Gosub helppressed                     ;toggle window open/closed
  111.       EndIf
  112.  
  113.   EndIf
  114. Until split=1
  115. End
  116.  
  117. ;--------------HELP WINDOW SUBROUTINE-----------------------------
  118.  
  119. ;Toggles help window open/closed
  120. ;and loads in the text file the first time it is called
  121.  
  122. .helppressed
  123.  
  124. If HelpMe.b=Off
  125.   If helploaded.b=0                          ;text not loaded yet
  126.     If Exists(help$)                         ;open the help doc
  127.       If ReadFile(0,help$)
  128.         ln=1
  129.         l.w=0
  130.  
  131.         FileInput 0
  132.  
  133.         While NOT Eof(0) AND l.w<maxlines
  134.           t$=Edit$(256)
  135.           check$=Left$(t$,1)
  136.           If check$="^"                      ;It's an INDEX!
  137.             t$=Mid$(t$,2)                    ;remove the character
  138.             lnk$(ln)=t$                      ;save link title
  139.             cy$=cy$+"|"+lnk$(ln)             ;add it to cycle gad
  140.             ln+1                             ;increase link count
  141.             check$=";"                       ;and don't show this line
  142.           EndIf
  143.           If check$=">"                      ;A link points here!
  144.             t$=Mid$(t$,2)
  145.             t1$=t$
  146.             If Left$(t1$,1)="}" Then t1$=Mid$(t1$,2)
  147.             For i=0 To ln                    ;check the list
  148.               If t1$=lnk$(i)                 ;found it's match!
  149.                 links(i)=l                   ;so update the array
  150.               EndIf
  151.             Next
  152.           EndIf
  153.  
  154.           If Left$(t$,1)="}"                 ;Center this line!
  155.             t1$=Mid$(t$,2)                   ;chuck the }
  156.             t$=" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
  157.             chars.w=Len(t$)
  158.             Gosub getmax
  159.             t$=t1$
  160.             If Len(t$)<maxchars
  161.               t$=Centre$(t$,maxchars)
  162.             EndIf
  163.           EndIf
  164.           If check$<>";"                     ;throw out comment lines
  165.             Repeat
  166.               t1$=""                         ;word wrap routine *gulp*
  167.               chars.w=Len(t$)                ;easy so far...
  168.               Gosub getmax
  169.               If chars>maxchars              ;OH,NO!
  170.                 For i=maxchars To 0 Step-1
  171.                   If Mid$(t$,i,1)=" "        ;look for a space
  172.                     t1$=Mid$(t$,i+1)         ;save the rest
  173.                     t$=Left$(t$,i-1)         ;trim t$
  174.                     i=0                      ;dump the loop
  175.                   EndIf
  176.                 Next i
  177.               EndIf
  178.               If AddItem (htxt())            ;add the line to the list
  179.                 htxt()\text=t$
  180.                 l+1
  181.               EndIf
  182.               If t1$<>"" Then t$=t1$         ;still more characters to add!
  183.             Until t1$=""                     ;keep going if more!
  184.           EndIf
  185.         Wend
  186.         CloseFile 0
  187.         helploaded=1
  188.         bw.w=(helpwide-8)/2
  189.       EndIf
  190.     Else
  191.       Request "Sorry...",help$+"|Not found."," OK "
  192.       helploaded=0
  193.     EndIf
  194.   EndIf
  195.   If helploaded=1
  196.     Gosub HelpGads
  197.     AddIDCMP $400000|$10|$400              ;need this for the arrows
  198.     Window #HelpWindow,helpx,helpy,helpwide,helphigh,$2|$8|$1000,"Help",1,0
  199.     AttachGTList #HelpWindow,#HelpWindow
  200.     GTSetAttrs #HelpWindow,50,#GTLV_Top,gt ;update the listview
  201.     SubIDCMP $400000|$10                   ;to prevent possible problems
  202.     HelpMe=On
  203.   EndIf
  204.  
  205. Else
  206.  
  207.   Use Window #HelpWindow
  208.   helpx=WindowX                            ;save current position
  209.   helpy=WindowY
  210.   Use Window 0                             ;and shut the window
  211.   Activate 0
  212.   Free Window #HelpWindow
  213.   Free GTList #HelpWindow
  214.   HelpMe=Off
  215. EndIf
  216. Return
  217.  
  218. .getmax
  219. maxchars=TextFit_(RastPort(0),&t$,chars,&al,0,1,txlim,fsize)
  220. Return
  221.  
  222. .HelpGads
  223.  
  224. GTTags #GTLV_ReadOnly,True
  225. GTListView #HelpWindow,50,0,0,helpwide-6,lvhigh,"",$10,htxt(),0,gt
  226. If HelpGadgets =2
  227.   GTButton #HelpWindow,51,0,lvhigh-fsize+4,bw,hghigh,"Go To ->",0
  228.   GTCycle #HelpWindow,52,(helpwide-6)/2,lvhigh-fsize+4,bw,hghigh,"",0,cy$,ec
  229. EndIf
  230. If HelpGadgets=1
  231.   GTCycle #HelpWindow,52,0,lvhigh-fsize+4,helpwide-6,hghigh,"",0,cy$,ec
  232. EndIf
  233. Return
  234.  
  235.